package top.huxy.boot.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.huxy.boot.mapper.ScheduleLogMapper;
import top.huxy.boot.model.dto.Pager;
import top.huxy.boot.model.dto.Query;
import top.huxy.boot.model.entity.ScheduleJobLog;
import top.huxy.boot.service.ScheduleLogService;
import top.huxy.boot.util.obj.Pagination;

import java.util.ArrayList;
import java.util.List;

/**
 * @Classname ScheduleLogServiceImpl
 * @Description ScheduleLogServiceImpl
 * @Date 2021/9/19 12:53
 * @Created by huxy
 */
@Service
public class ScheduleLogServiceImpl implements ScheduleLogService {

    @Autowired
    ScheduleLogMapper scheduleLogMapper;

    @Override
    @Deprecated
    public int getScheduleLogCount() {
        return scheduleLogMapper.getScheduleLogCount();
    }

    @Override
    @Deprecated
    public List<ScheduleJobLog> getScheduleLogList(Pagination pagination) {
        return scheduleLogMapper.getScheduleLogList(pagination.getLeftIndex(), pagination.getRightIndex());
    }

    @Override
    public int saveLog(ScheduleJobLog scheduleJobLog) {
        return scheduleLogMapper.saveLog(scheduleJobLog);
    }

    @Override
    public PageInfo<ScheduleJobLog> getScheduleLogListByFilter(Pager pager, Query query) {
        query
                .allowKey("ids", false)
                .replaceValue("ids", str -> {
                    ArrayList<Integer> res = new ArrayList<>();
                    for (String s : str.split(",")) {
                        res.add(Integer.parseInt(s));
                    }
                    return res;
                })

                .allowKey("status", false)
                .replaceValue("status", Byte::parseByte)

                .allowKey("startTime", false)
                .replaceValue("startTime", Long::parseLong)

                .allowKey("endTime", false)
                .replaceValue("endTime", Long::parseLong)
        ;

        PageHelper.startPage(pager.getPage(), pager.getPageSize(), "create_time desc");
        return new PageInfo<>(scheduleLogMapper.getScheduleLogListByFilter(query.getQueryMap()));
    }

    @Override
    public List<Integer> getScheduleLogJobIds() {
        return scheduleLogMapper.getScheduleLogJobIds();
    }
}
